<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<base target="_self" />
		<title>添加角色信息</title>
		<%@ include file="../jslib/jquerylib.jsp" %> 
	    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/web/style/layout4.css" />
		<script type='text/javascript' src="<%=request.getContextPath() %>/jslib/jquery.form.js"></script>
		<link href="<%=request.getContextPath() %>/web/style/layout.css" type="text/css" rel="stylesheet"/>
        <link rel="stylesheet" href="<%=request.getContextPath() %>/jslib/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css"/>
		<script type="text/javascript" src="<%=request.getContextPath() %>/jslib/lhgdialog/lhgcore.lhgdialog.min.js?skin=mac"></script>
        <script type="text/javascript" src="<%=request.getContextPath() %>/jslib/ztree/jquery.ztree.core-3.4.js"></script>

		<style type="text/css">
				html{overflow-x:hidden;}
		</style>
		<style type="text/css">
			.tdtitle
			{
				background-color:#b7dbf3;
				text-align:center;
				height:20px;
				font-size:15px;
				font-weight:bold;
			}
			
		</style>
		<c:if test="${requestScope.message!=null}">
			<script type="text/javascript">
  	 		     alert('<c:out value="${requestScope.message}"></c:out>');
  		</script>
	    </c:if>
		<script type="text/javascript">

	 var api = frameElement.api, W = api.opener;
	function init(){
		//alert(aaa);
		//var obj = window.dialogArguments;
		var parentid = document.getElementById("parentid");		
		//var rolename = document.getElementById("rolename");
		var parentName = document.getElementById("parentName");
		//var rolelabel = document.getElementById("rolelabel");
		//var roledesc = document.getElementById("roledesc");

		parentName.value = eval("("+api.data+")").parentName;
		//rolename.value = eval("("+api.data+")").rolename;
		//rolelabel.value=eval("("+api.data+")").rolelabel;
		//roledesc.value= eval("("+api.data+")").roledesc;
		parentid.value = eval("("+api.data+")").parentid;	
	}
  
 	$().ready(function() {
		// 准备好Options对象
		var options = {
			url:'saveRole.action',
			beforeSubmit:checkForm,
			dataType:'json',
			success: function(data) {
			//alert(111);
				if(data.ajaxResult == 'success'){
					alert("添加成功！");
					api.close();
					W.reload();
				}else {
					alert("添加失败，请重试！");
					api.close();
				}
				//location.reload();
			}};
		 $('#saveBtn').click(function(){
			   // var temp = $('#pageFrom')
		    	$('#pageFrom').ajaxSubmit(options);
		            return false;
	    	 });
			$('#closeBtn').click(function(){
				api.close();
	    	 });
	});	

    //校验 
	function checkForm(){
		var flag = true;
		if($.trim($("#rolename").val())==""){
			alert("角色名称不能为空!");
			$("#rolename").focus();
			return false;
		}else if(!checkExp($("#rolename").val())){
			alert("角色名称不能含有如下特殊符号：\" # $ \, \'");
			$("#rolename").focus();
			return false;
		}else if(!(flag = checkId())){
			$("#rolename").focus();
			return false;
		}else if(!roleNameCountintlen($("#rolename").val())){
			alert("角色名称最多为40个字符,字符输入超出限制!");
			$("#rolename").focus();
			return false;
		}
		
		if($("#rolelabel").val()=="0"){
			alert("请选择角色标识!");
			$("#rolelabel").focus();
			return false;
		}

		if(!checkExp($("#roledesc").val())){
			alert("角色描述不能含有如下特殊符号：\" # $ \, \'");
			$("#roledesc").focus();
			return false;
		}else if(!rolelabelCountintlen($("#roledesc").val())){
			alert("角色描述最多为100个字符,字符输入超出限制!");
			$("#roledesc").focus();
			return false;
		}
		return flag;
	}

	 function checkId(){
			var flag = true;
			$.ajax({
			   type: "POST",
			   async: false,//取消异步提交
			   url: "getRolesName.action",
			   dataType: "json",
			   data: {
				      rolename: function(){
						 return encodeURIComponent($("#rolename").val());
					}
			   },
			   success: function(data){
			   	 if(data.result != "success"){
			     	alert( "角色名称已存在，请重新输入！" );
			     	flag = false;
			     }
			   }
			}); 
			return flag;
		}
	 function roleNameCountintlen(input){
			var result = $("#rolename").val().length;
			if (result > 40) {
				return false;
			}else{
				return true;
			}
	}
	 function rolelabelCountintlen(input){
			var result = $("#roledesc").val().length;
			if (result > 100) {
				return false;
			}else{
				return true;
			}
	}
	//特殊字符验证
		function checkExp(inputData){ 
			//var regEx = new RegExp(/^(([^\^\.<>%&',:;=?$"'#@!~\]\[{}【】￥（）！·‘’”“。，、？：；—|\/\\/`\|])*)$/);
			var regEx = new RegExp(/^(([^$,"'#])*)$/);
			var result = inputData.match(regEx);
			if (result == null) {
				return false;
			}
			return true;
		}

	//过滤表单的前后空格
	function trimkeyup(e) {
	     lucene_objInput = $(this);
	     if (e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13) {
	        var im = $.trim(lucene_objInput.val());
	       lucene_objInput.val(im); 
	    }
	}
 </script>
  	<style type="text/css">
 		body{
 			background:#ffffff; background-image:url(images/);
 		}
   		html{
		    overflow-x: hidden;
			overflow-y: hidden;
		}  		
   </style>
	</head>
	<body onload="init()">	
		<form action="" id="pageFrom" name="" method="post" >
			<input type="hidden" id="parentid" name="parentid"/>
			<table width="100%" border="0" align="center" cellpadding="5" cellspacing="5" class="formTableNew">
				<tr>
					<td align="right">父角色名称：</td>
					<td align="left">
						<div class="div">
							<input type="text" id="parentName" name="parentName" readonly="readonly" style="width:250px" />
						</div>
					</td>	
				</tr>
				<tr>
					<td align="right">角色名称：</td>
					<td >
					<div class="div">
						<input id="rolename" name="rolename" style="width:250px" />
						<img src="<%=request.getContextPath()%>/images/xh.png"></img>
						</div>
					</td>
				</tr>
				<tr>
					<td align="right">角色标识：</td>
					<td>
					<div class="div">
                        <select name="rolelabel" id="rolelabel" style="width:262px" >
                            <option value="0">请选择</option>
                            <option value="ROLE_SYSADMIN">系统管理员</option>
                            <option value="ROLE_ADMIN">用户管理员</option>
                            <option value="ROLE_OPERATOR">操作员</option>
                            <option value="ROLE_MAINTENANCE">研究员</option>
                        </select>
                        <img src="<%=request.getContextPath()%>/images/xh.png" alt="" />
                        </div>
                    </td>
				</tr>
				<tr>
					<td align="right">
						角色描述：
					</td>
					<td>
						<div class="div">
							<textarea rows="8" cols="50" id="roledesc" name="roledesc" style="width:250px"></textarea>
						</div>
					</td>
				</tr>
					<tr>
					<td align="center" colspan="2">
						<input type="button" id="saveBtn" class="form_bt_orange"  value="保存"/>
						<input type="button" id="closeBtn" class="form_bt_grey" value="取消 "/>
					</td>
				</tr>
			</table>	
		</form>
						
	</body>
</html>
